package anji.plus.system.mark.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * <p>
 * 通用数据响应结果
 * </p>
 *
 * @author zag
 * @since 2023/7/14
 */
@Data
public class ResponseResult implements Serializable {

    private static final long serialVersionUID = 6209490523720730172L;

    private Integer code;

    private String message;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String throwable;

    private Object data;

    public ResponseResult() {
        super();
    }

    /**
     * 一般的响应结果，不返回数据
     * @param code {@link ResponseCode}
     */
    public ResponseResult(ResponseCode code) {
        super();
        this.code = code.code();
        this.message = code.message();
    }

    /**
     * 一般的响应结果，不返回数据,自定义消息
     * @param code {@link ResponseCode}
     * @param message {@link String} 自定义消息
     */
    public ResponseResult(ResponseCode code, String message) {
        super();
        this.code = code.code();
        this.message = message;
    }

    /**
     * 一般的响应结果，返回数据
     * @param code {@link ResponseCode}
     * @param data {@link Object}
     */
    public ResponseResult(ResponseCode code, Object data) {
        super();
        this.code = code.code();
        this.message = code.message();
        this.data = data;
    }

    /**
     * 异常的响应结果，返回的对象会包含{@code throwable} 字段
     * @param code {@link ResponseCode}
     * @param throwable {@link Throwable}
     */
    public ResponseResult(ResponseCode code, Throwable throwable) {
        super();
        this.code = code.code();
        this.message = code.message();
        this.throwable = throwable.getMessage();
    }

    /**
     * 请求成功
     */
    public static ResponseResult success() {
        return new ResponseResult(ResponseCode.SUCCESS);
    }

    /**
     * 请求成功
     * @param message {@code String} 自定义消息
     */
    public static ResponseResult success(String message) {
        return new ResponseResult(ResponseCode.SUCCESS, message);
    }

    /**
     * 请求成功
     * @param data {@code String} 返回数据
     */
    public static ResponseResult success(Object data) {
        return new ResponseResult(ResponseCode.SUCCESS, data);
    }

    /**
     * 请求失败
     *
     */
    public static ResponseResult failure() {
        return new ResponseResult(ResponseCode.FAILURE);
    }

    /**
     * 请求失败
     * @param code {@link ResponseCode}
     */
    public static ResponseResult failure(ResponseCode code) {
        return new ResponseResult(code);
    }

    /**
     * 请求失败
     * @param message {@code String} 自定义消息
     */
    public static ResponseResult failure(String message) {
        return new ResponseResult(ResponseCode.FAILURE, message);
    }

    /**
     * 请求失败
     * @param throwable {@code String} 异常信息
     */
    public static ResponseResult failure(Throwable throwable) {
        return new ResponseResult(ResponseCode.FAILURE, throwable);
    }

}
